package net.imwork.amorgemini.core.aspect;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;
import net.imwork.amorgemini.core.common.Result;
import net.imwork.amorgemini.core.dict.ExceptionDict;
import net.imwork.amorgemini.core.exception.BaseException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.Charset;

/**
 * Created by lvbr on 2017-09-01 19:40.
 */
@Aspect
@Configuration
public class ControllerAspect {
    @Pointcut("execution(* net.imwork.amorgemini.controller..*.*(..))")
    public void result() {
    }

    @AfterReturning(returning = "ret", pointcut = "result()")
    public void doAfterReturning(Object ret) {
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        Result result = new Result();
        result.setCode(Result.SUCCESS_CODE);
        result.setMsg(Result.SUCCESS_MSG);
        result.setData(ret);
        HttpOutputMessage outputMessage = new ServletServerHttpResponse(response);
        FastJsonHttpMessageConverter4 converter4 = new FastJsonHttpMessageConverter4();
        try {
            SerializerFeature[] serializerFeatures = {SerializerFeature.WriteMapNullValue};
            FastJsonConfig config = new FastJsonConfig();
            config.setSerializerFeatures(serializerFeatures);
            config.setDateFormat("yyyy-MM-dd hh:mm:ss");
            converter4.setFastJsonConfig(config);
            converter4.write(result, MediaType.APPLICATION_JSON, outputMessage);
        } catch (HttpMessageNotWritableException | IOException e) {
            e.printStackTrace();
        }
    }

    @AfterThrowing(pointcut = "result()", throwing = "e")
    public void afterThrowing(JoinPoint jp, Throwable e) {
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        Result result = new Result();
        if (e instanceof BaseException) {
            result.setCode(((BaseException) e).getCode());
            result.setMsg(e.getMessage());
            HttpOutputMessage outputMessage = new ServletServerHttpResponse(response);
            FastJsonHttpMessageConverter4 converter4 = new FastJsonHttpMessageConverter4();
            try {
                converter4.setDefaultCharset(Charset.defaultCharset());
                converter4.write(result, MediaType.APPLICATION_JSON, outputMessage);
            } catch (HttpMessageNotWritableException | IOException e1) {
                e1.printStackTrace();
            }
        } else {
            result.setCode(ExceptionDict.UNKNOW_ERROR.getCode());
            result.setMsg(ExceptionDict.SYSTEM_ERROR.getMsg());
            result.setData(e.getMessage());
            HttpOutputMessage outputMessage = new ServletServerHttpResponse(response);
            FastJsonHttpMessageConverter4 converter4 = new FastJsonHttpMessageConverter4();
            try {
                converter4.setDefaultCharset(Charset.defaultCharset());
                converter4.write(result, MediaType.APPLICATION_JSON, outputMessage);
            } catch (HttpMessageNotWritableException | IOException e1) {
                e1.printStackTrace();
            }
        }
    }
}
